#include "bsp.h"

const uint16_t ledPin[5] = {PIN_LED1, PIN_LED2, PIN_LED3, PIN_LED4, PIN_LED5};
GPIO_Module * const ledPort[5] = {PORT_LED1, PORT_LED2, PORT_LED3, PORT_LED4, PORT_LED5};

void ledSocDisplay(uint16_t state, uint16_t soc)
{
    static uint16_t socLevel = 4;

    switch (socLevel) {
        case 0:
            if (soc > 205)
            {
                socLevel = 1;
            }
            break;

        case 1:
            if (soc > 405) {
                socLevel = 2;
            } else if (soc < 195) {
                socLevel = 0;
            }

            break;

        case 2:
            if (soc > 605) {
                socLevel = 3;
            } else if (soc < 395) {
                socLevel = 1;
            }
            break;

        case 3:
            if (soc > 805) {
                socLevel = 4;
            } else if (soc < 595) {
                socLevel = 2;
            }
            break;

        case 4:
            if (soc < 795) {
                socLevel = 3;
            }
            break;
            
        default:
            socLevel = 0;
            break;
    }

    for ( uint16_t i = 0; i < socLevel; i++)
    {
        bsp_gpio_set(ledPort[i], ledPin[i]);
    }

    if (state == 1) // 放电
    {
        bsp_gpio_toggle(ledPort[socLevel], ledPin[socLevel]);
    } else {
        bsp_gpio_set(ledPort[socLevel], ledPin[socLevel]);
    }
    
    for ( uint16_t i = socLevel + 1; i < 5; i++)
    {
        bsp_gpio_reset(ledPort[i], ledPin[i]);
    }    
}


void ledAlmDisplay(uint16_t fault)
{
    if (fault == 0) {
        bsp_gpio_set(PORT_LED_ALM, PIN_LED_ALM);
    } else {
        bsp_gpio_reset(PORT_LED_ALM, PIN_LED_ALM);
    }
}

void ledAllBlink(void)
{
    static uint16_t ledState = 0;

    if (ledState == 0) {
        bsp_gpio_reset(PORT_LED_ALM, PIN_LED_ALM);
        for ( uint16_t i = 0; i < 5; i++)
        {
            bsp_gpio_reset(ledPort[i], ledPin[i]);
        }
        ledState = 1;
    } else {
        bsp_gpio_set(PORT_LED_ALM, PIN_LED_ALM);
        for ( uint16_t i = 0; i < 5; i++)
        {
            bsp_gpio_set(ledPort[i], ledPin[i]);
        }
        ledState = 0;
    }
}

void ledAllReset(void)
{
    bsp_gpio_reset(PORT_LED_ALM, PIN_LED_ALM);
    for ( uint16_t i = 0; i < 5; i++)
    {
        bsp_gpio_reset(ledPort[i], ledPin[i]);
    }
}